package kr.util.extractor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class UrlExtractor extends AbstractExtractor{

	private TagNode node = null;
	
	public UrlExtractor(String fileName){
		super(fileName);
	}
	
	public UrlExtractor(String fileName, String charset){
		super(fileName, charset);
	}
	
	@Override
	public void setText(String fileName) {
		setNode(fileName, this.charset);
	}
	
	public TagNode getNode() {
		return node;
	}

	public void setNode(String fileName){
		setNode(fileName, null);
	}
	
	public void setNode(String fileName, String charset) {
		this.fileName = fileName;
		URL url = null;
		try {
			url = new URL(fileName);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		InputStreamReader ir = null;
		try {
			if(charset == null) ir = new InputStreamReader(url.openStream());
			else ir = new InputStreamReader(url.openStream(), charset);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		BufferedReader br = new BufferedReader(ir);
		HtmlCleaner cleaner = new HtmlCleaner();
		CleanerProperties props = cleaner.getProperties();
		props.setOmitComments(true);
		try {
			this.node = cleaner.clean(br);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.text = this.node.getText();
		
		try {
			ir.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		UrlExtractor ue = new UrlExtractor("http://docs.oracle.com/javase/6/docs/api/overview-summary.html");
		System.out.println(ue.getText().toString());
	}
}